<?php
class ModelCatalogTestimonial extends Model {
	public function getTestimonial($testimonial_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "testimonial t LEFT JOIN " . DB_PREFIX . "testimonial_description td ON (t.testimonial_id = td.testimonial_id) WHERE t.testimonial_id = '" . (int)$testimonial_id . "' AND td.language_id = '" . (int)$this->config->get('config_language_id') . "' AND t.status = '1'");
	
		return $query->row;
	}
	
	public function getTestimonials($start = 0, $limit = 20) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "testimonial t LEFT JOIN " . DB_PREFIX . "testimonial_description td ON (t.testimonial_id = td.testimonial_id) WHERE td.language_id = '" . (int)$this->config->get('config_language_id') . "' AND t.status = '1' ORDER BY t.sort_order ASC LIMIT " . (int)$start . "," . (int)$limit);
	
		return $query->rows;
	}
	
	public function getTotalTestimonials() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "testimonial t LEFT JOIN " . DB_PREFIX . "testimonial_description td ON (t.testimonial_id = td.testimonial_id) WHERE td.language_id = '" . (int)$this->config->get('config_language_id') . "' AND t.status = '1'");
		
	
		return $query->row['total'];
	}
	
	
	public function addTestimonial($data) {
		$this->db->query("INSERT INTO `" . DB_PREFIX . "testimonial` SET sort_order = 0, status = 0");

		$testimonial_id = $this->db->getLastId(); 
		
		$language_code = $this->session->data['language'];
		$query = $this->db->query( "SELECT language_id FROM `".DB_PREFIX."language` WHERE code = '$language_code'" );
		if ($query->num_rows) {
			$language_id = $query->row['language_id'];
		} else {
			$language_id = 1;
		}

		$title = $data['name'] . date( " - j M Y" );
		$description = str_replace( "\n", "<br />\n", $data['feedback'] );
		$description = str_replace( "\r", "", $description );
		$this->db->query("INSERT INTO `" . DB_PREFIX . "testimonial_description` SET testimonial_id = '" . (int)$testimonial_id . "', language_id = '".(int)$language_id."', title = '" . $this->db->escape($title) . "', description = '" . $this->db->escape($description) . "'");

		$this->cache->delete('testimonial');
	}
}
?>